'use strict';

angular.module('myApp.competition_base', [])

.controller('competition_baseCtrl', ['$scope','$routeParams','$http',
    function($scope,$routeParams,$http) {
	$http.get('v1/competition/'+$routeParams.id).success(function(data){
		$scope.competition = data.message['base']['0'];
		$scope.comd = $routeParams.id;
	});
	//组别
	$http.get('v1/competition/'+$routeParams.id+ '/group').success(function(data){
		$scope.groups= data.result;
		// console.log(data);
	});
	$("#group_button").on('click',function(){
		var key = $("#group_input").val();
		if (key != '') {
			var new_group = {'name' : key};
		    $http.post('v1/competition/'+$routeParams.id+'/group',new_group).success(function(data){
	            if(!data.error ){
			    $http.get('v1/competition/'+$routeParams.id+ '/group').success(function(data){
			        $scope.groups = data.result;
			    });
	            }else{
	                alert(data.message);
	            }
		   });
	   	};
	});
	$scope.delg = function(group){
		console.log(group.id);
		    $http.delete('v1/competition/'+$routeParams.id+'/group/'+group.id).success(function(data){
		    	console.log(data);
				if( !data.error ){
					$scope.groups.splice($scope.groups.indexOf(group),1);
					alert("删除成功","success");
				}else{
					alert(data.message);
				}
		    });
    	
	};
	//奖项
	$http.get('v1/competition/'+$routeParams.id+ '/award').success(function(data){
		$scope.awards= data.result;
		// console.log(data);
	});	
	$("#award_button").on('click',function(){
		var key = $("#award_input").val();
		if (key != '') {
			var new_award = {'name' : key};
		    $http.post('v1/competition/'+$routeParams.id+'/award',new_award).success(function(data){
	            if(!data.error ){
			    $http.get('v1/competition/'+$routeParams.id+ '/award').success(function(data){
			        $scope.awards = data.result;
			    });
	            }else{
	                alert(data.message);
	            }
		   });
	   	};
	});
	$scope.dela = function(award){
	    $http.delete('v1/competition/'+$routeParams.id+'/award/'+award.id).success(function(data){
			if( !data.error ){
				$scope.awards.splice($scope.awards.indexOf(award),1);
				alert("删除成功","success");
			}else{
				alert(data.message);
			}
	    });
	};
   

   //住宿
   	$http.get('v1/competition/'+$routeParams.id+'/room').success(function(data){
		var temp_result = data.result;
		 console.log(temp_result);
		var temp_result_length = temp_result.length;
		for( var i=0; i<temp_result_length ; i++ ){
			if( temp_result[i].is_alone == 1 )
				temp_result[i].is_alone = '单住';
			else
				temp_result[i].is_alone = '合床';
		}
		$scope.rooms = temp_result;
		console.log(data);
	});
	//住宿添加
    $scope.ok = function(addroom){ 
        addroom.cp_id = $routeParams.id;
        $scope.tem = angular.copy(addroom);

         console.log( $scope.tem);
        $http.post('v1/competition/'+ $routeParams.id+'/room',$scope.tem).success(function(data){

            if( !data.error ){
                alert("添加成功","success");
			   	$http.get('v1/competition/'+$routeParams.id+'/room').success(function(data){
					var temp_result = data.result;
					 console.log(temp_result);
					var temp_result_length = temp_result.length;
					for( var i=0; i<temp_result_length ; i++ ){
						if( temp_result[i].is_alone == 1 )
							temp_result[i].is_alone = '单住';
						else
							temp_result[i].is_alone = '合床';
					}
					$scope.rooms = temp_result;
				});
            }else{
                alert(data.message);
            }
        });
    };

    //比赛基本信息提交
    $scope.submit = function(competition){ 
        competition.id = $routeParams.id;
        competition.id_published = 0;
        $scope.tem = angular.copy(competition);
         console.log( $scope.tem);
        $http.put('v1/competition/'+ $routeParams.id,$scope.tem).success(function(data){
            if( !data.error ){
                alert("修改成功","success");
                window.location.href = '#/competition_new';
            }else{
                alert(data.message);
            }
        });
    };    


   $('.form-datetime').datetimepicker(
    {
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: 'yyyy-mm-dd hh:ii'
    });
    // 日历
    $('.form-date').datetimepicker(
    {
        language:  'zh-CN',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0,
        format: 'yyyy-mm-dd'
    });
}]);